package cn.nebula.music.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.nebula.music.domain.Role;
import cn.nebula.music.service.RoleService;
import cn.nebula.music.service.UserService;


@Controller
public class RoleController {

	@Autowired
	private RoleService roleService;
	
	@Autowired
	private UserService userService;

	@RequestMapping("/editRole")
	@ResponseBody
	public Map<String,Object> editRole(@RequestParam Integer id) throws Exception {
		Map<String,Object> values = new HashMap<String,Object>();
		values.put("role", roleService.findRoleById(id));
		values.put("users", userService.findAllUsersOfBT());
		return values;
	}
	
	@RequestMapping("/listAllRoles")
	@ResponseBody public Map<String, Object> listAllRoles() throws Exception {
		Map<String,Object> values = new HashMap<String, Object>();
		values.put("roles",roleService.findAllRolesOfBT());
		return values;
	}
	
	@RequestMapping("/newRole")
	@ResponseBody
	public Map<String,Object> newRole() throws Exception {
		Map<String,Object> values = new HashMap<>();
		values.put("users", userService.findAllUsersOfBT());
		return values;
	}
	
	@RequestMapping("/updateRole")
	@ResponseBody
	public boolean updateRole(@RequestBody Role role) throws Exception {
		roleService.updateRole(role);
		return true;
	}
	
	@RequestMapping("/addRole")
	@ResponseBody
	public boolean addRole(@RequestBody Role role) throws Exception {
		roleService.addRole(role);
		return true;
	}
	
	@RequestMapping("/deleteRole")
	@ResponseBody
	public boolean deleteRole(@RequestParam Integer id) throws Exception {
		roleService.deleteRoleById(id);
		return true;
	}
}
